import React from 'react'
import rely from '../../../oauth2rely'
import app from '../../../../app'

class Callback extends React.Component {
    static contextTypes = {
        store: React.PropTypes.object,
        router: React.PropTypes.object
    };

    constructor(props, context) {
        super(props, context);
        // this.state = {
        //   code: ''
        // }
    }

    componentWillMount() {
        let authResult = rely.resolveAuthCallback();
        //this.setState(authResult);

        // 请求token
        if (authResult.result)
            rely.token(authResult.code, this.tokenSuccess, this.error);
        else
            this.error(authResult.error);
    }


    render() {
        return (
            <div><p>登录信息处理中</p><p>请稍候</p></div>
        );
    }

    error = (err) => {
        app.showError(err);
    };

    tokenSuccess = (access_token) => {
        rely.userInfo(access_token, this.userInfoSuccess, this.error)
    };

    userInfoSuccess = () => {
        rely.returnToRedirectUrl(this.props.router.replace);
    };
}

export default Callback